﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FDEE.Model
{
    public class Excel2CADRebar : MemberBase
    {
        System.Data.DataTable dt;
        public Excel2CADRebar(string typeName, string pierNumber, System.Data.DataTable _dt)
            : base(typeName, pierNumber)
        {
            dt = _dt;
        }
        public override void SetConcreteClass()
        {
            ConcreteClass = eConcreteClass.C45;
        }

        public override void SetConcreteVolume()
        {
            ConcreteVolumn = 0;
        }

        public override void SetRebarList()
        {
            RebarList = new RebarCollection();

            for (int n = 0; n < dt.Rows.Count; n++)
            {
                try
                {
                    System.Data.DataRow dr = dt.Rows[n];
                    string shape = dr["Shape"].ToString();
                    string barMark = dr["BarMark"].ToString();
                    string rDB = dr["RefDiameter"].ToString();
                    eDB db = (FDEE.Model.eDB)Enum.Parse(typeof(FDEE.Model.eDB), rDB);
                    eShapeType shp = (FDEE.Model.eShapeType)Enum.Parse(typeof(FDEE.Model.eShapeType), shape);
                    double R = 0, A = 0, B = 0, C = 0, D = 0, E = 0, ExR = 0, ExA = 0, ExB = 0, ExC = 0, ExD = 0, ExE = 0;
                    int quantity = 1;
                    if (!dr.IsNull("R"))
                        R = double.Parse(dr["R"].ToString());
                    if (!dr.IsNull("A"))
                        A = double.Parse(dr["A"].ToString());
                    if (!dr.IsNull("B"))
                        B = double.Parse(dr["B"].ToString());
                    if (!dr.IsNull("C"))
                        C = double.Parse(dr["C"].ToString());
                    if (!dr.IsNull("D"))
                        D = double.Parse(dr["D"].ToString());
                    if (!dr.IsNull("E"))
                        E = double.Parse(dr["E"].ToString());
                    if (!dr.IsNull("ExR"))
                        ExR = double.Parse(dr["ExR"].ToString());
                    if (!dr.IsNull("ExA"))
                        ExA = double.Parse(dr["ExA"].ToString());
                    if (!dr.IsNull("ExB"))
                        ExB = double.Parse(dr["ExB"].ToString());
                    if (!dr.IsNull("ExC"))
                        ExC = double.Parse(dr["ExC"].ToString());
                    if (!dr.IsNull("ExD"))
                        ExD = double.Parse(dr["ExD"].ToString());
                    if (!dr.IsNull("ExE"))
                        ExE = double.Parse(dr["ExE"].ToString());
                    if (!dr.IsNull("Quantity"))
                        quantity = int.Parse(dr["Quantity"].ToString());
                    EXNumber ex = new EXNumber();
                    ex.exNumber["A"] = ExA;
                    ex.exNumber["B"] = ExB;
                    ex.exNumber["C"] = ExC;
                    ex.exNumber["D"] = ExD;
                    ex.exNumber["E"] = ExE;
                    ex.exNumber["R"] = ExR;

                    int mainBar = 1, sBar = 0;
                    string cBar = "";
            
                    string doubleNumber = double.Parse(Regex.Replace(barMark, @"[^0-9,.]+", "")).ToString("0.0");
                   
                    Regex r = new Regex(@"[a-zA-Z]+");
                    Match m = r.Match(barMark);
                    cBar = m.Value;
                    List<string> bar = doubleNumber.Split('.').ToList();
                    if (bar.Count > 0)
                    {
                        mainBar = int.Parse(bar[0]);
                        if (bar.Count > 1)
                            sBar = int.Parse(bar[1]);
                    }
                    int rr = 0;
                    switch (shp)
                    {
                        case eShapeType.MA:
                            RebarList.Add(new MARebarRecord(mainBar, sBar, cBar, db, A, quantity, shp, ex));
                            break;
                        case eShapeType.MAB:
                        case eShapeType.MBA:
                            RebarList.Add(new MABRebarRecord(mainBar, sBar, cBar, db, A, B, quantity, shp, ex));
                            break;
                        case eShapeType.MABC:
                            RebarList.Add(new MABCRebarRecord(mainBar, sBar, cBar, db, A, B, C, quantity, shp, ex));
                            break;
                        case eShapeType.MAR:
                            RebarList.Add(new MARRebarRecord(mainBar, sBar, cBar, db, A, R, quantity, shp, ex));
                            break;
                        case eShapeType.MABR:
                        case eShapeType.MBAR:
                            RebarList.Add(new MABRRebarRecord(mainBar, sBar, cBar, db, A, B, R, quantity, shp, ex));
                            break;
                        case eShapeType.MABCR:
                            RebarList.Add(new MABCRRebarRecord(mainBar, sBar, cBar, db, A, B, C, R, quantity, shp, ex));
                            break;
                        case eShapeType.MAU:
                            RebarList.Add(new MAURebarRecord(mainBar, sBar, cBar, db, A, B, C, quantity, shp, ex));
                            break;
                        case eShapeType.MUN:
                            RebarList.Add(new MUNRebarRecord(mainBar, sBar, cBar, db, quantity, shp, A, B, C));
                            break;
                        case eShapeType.MUL:
                            RebarList.Add(new MULRebarRecord(mainBar, sBar, cBar, db, quantity, shp, A, B, C, D, E));
                            break;
                        case eShapeType.MUA:
                            RebarList.Add(new MUARebarRecord(mainBar, sBar, cBar, db, quantity, shp, A, B, C, D, ex));
                            break;
                        case eShapeType.MLL:
                            rr = RebarKnowledge.GerRadius(db);
                            RebarList.Add(new MLADDRebarRecord(mainBar, sBar, cBar, db, quantity, shp, A, B, rr));
                            break;
                        case eShapeType.MLR:
                            rr = RebarKnowledge.GerRadius(db);
                            RebarList.Add(new MLADDRebarRecord(mainBar, sBar, cBar, db, quantity, shp, A, B, rr));
                            break;
                        case eShapeType.MO:
                            RebarList.Add(new MORebarRecord(mainBar, sBar, cBar, db, quantity, shp, A, B));
                            break;
                        case eShapeType.MSZ:
                            RebarList.Add(new MSZRebarRecord(mainBar, sBar, cBar, db, quantity, shp, A, B, C));
                            break;
                        case eShapeType.MCT:
                            RebarList.Add(new MCRebarRecord(mainBar, sBar, cBar, db, A, B, C, quantity, shp));
                            break;
                        case eShapeType.MCB:
                            RebarList.Add(new MCRebarRecord(mainBar, sBar, cBar, db, A, B, C, quantity, shp));
                            break;
                        case eShapeType.MCL:
                            RebarList.Add(new MCRebarRecord(mainBar, sBar, cBar, db, A, B, C, quantity, shp));
                            break;
                        case eShapeType.MCR:
                            RebarList.Add(new MCRebarRecord(mainBar, sBar, cBar, db, A, B, C, quantity, shp));
                            break;
                    }
                }
                catch (Exception ex)
                {

                }
                //RebarList.Sort();

                //RebarList.Add(new MORebarRecord(1, 0, station, eDB.DB12, N3, eShapeType.MO, aa, bb));
            }
           
        }
    }
}
